package com.hrrt.home.socket;

import com.hrrt.home.socket.model.MsgData;
import com.hrrt.home.socket.model.MsgHead;
import com.hrrt.home.socket.utils.BatyIntUtil;
import com.hrrt.home.socket.utils.ByteTwo;
import com.hrrt.home.socket.utils.CRC16;

import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static final int PORT = 18080;//监听的端口号

    public static void main(String[] args) {
        System.out.println("服务器启动...\n");
        Server server = new Server();
        server.init();

//        String str = "0x11";
//        int a = Integer.parseInt(str);
//        System.out.println(a);
    }

    public void init() {
        try {
            ServerSocket serverSocket = new ServerSocket(PORT);
            while (true) {
                // 一旦有堵塞, 则表示服务器与客户端获得了连接
                Socket client = serverSocket.accept();
                // 处理这次连接
                new HandlerThread(client);
            }
        } catch (Exception e) {
            System.out.println("服务器异常: " + e.getMessage());
        }
    }

    private class HandlerThread implements Runnable {
        private Socket socket;
        public HandlerThread(Socket client) {
            socket = client;
            new Thread(this).start();
        }

        public void run() {
            try {
                DataOutputStream out = new DataOutputStream(socket.getOutputStream());
//                while (true) {
//                    Thread.sleep(5000);
                    MsgHead msgHead = new MsgHead();
                    msgHead.setId(56);//消息ID
                    msgHead.setMac_id("87905509005909590990");
                    msgHead.setMac_idt(2);
                    msgHead.setnMsg(1234);//流水号
                    msgHead.setXxbcount(11223344);
                    msgHead.setXx(11223344);//封装项
                    MsgData msgData = new MsgData();
                    msgData.setMode(65);//消息模式
                    msgData.setN(1);//控制设备批次
                    msgData.setObj(23);
                    msgData.setType(51);
                    msgData.setFtype(11);
                    msgData.setText(1107);
                    msgData.setData("12345678901234567890123456temp26");//消息踢指令数据
                    msgHead.setProperties(msgData.getData().getBytes().length);//消息属性
                    String a = msgHead.toString()+msgData.toString();

                    byte[] bytes1=ByteTwo.byteMerger(BatyIntUtil.intToBytes(msgHead.getId()),BatyIntUtil.intToBytes(msgData.getData().getBytes().length),msgHead.getMac_id().getBytes(),BatyIntUtil.intToBytes(msgHead.getMac_idt()),BatyIntUtil.intToBytes(msgHead.getnMsg()),BatyIntUtil.intToBytes(msgHead.getXx()),BatyIntUtil.intToBytes(msgHead.getXxbcount()),BatyIntUtil.intToBytes(msgData.getMode()),BatyIntUtil.intToBytes(msgData.getN()),BatyIntUtil.intToBytes(msgData.getObj()),BatyIntUtil.intToBytes(msgData.getType()),BatyIntUtil.intToBytes(msgData.getFtype()),BatyIntUtil.intToBytes(msgData.getText()),msgData.getData().getBytes());
                    int count = CRC16.calcCrc16(bytes1);
                    System.out.println("校验值"+count);
                    byte [] bytes11=ByteTwo.byteMerger(BatyIntUtil.intToBytes(68),bytes1,BatyIntUtil.intToBytes(count),BatyIntUtil.intToBytes(68));
                    out.write(bytes11);
                    out.flush();
//                }




            } catch (Exception e) {
                System.out.println("服务器 run 异常: " + e.getMessage());
            } finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (Exception e) {
                        socket = null;
                        System.out.println("服务端 finally 异常:" + e.getMessage());
                    }
                }
            }
        }
    }
}

